<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\ModelList;
use App\Models\ModelVersion;
use Illuminate\Http\Request;
use App\Services\ToolsService;
use Illuminate\Support\Facades\DB;
use Matrix\Exception;

class ModelController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //$this->middleware('auth');
    }

    /**
     * 获取待训练详情
     */
    public function getInfo(Request $request){
        try{
            DB::beginTransaction();
            $data = [];
            //获取待训练详情
            $info = ModelVersion::where('status',0)->lockForUpdate()->first();
            if($info){
                $data['id'] = $info->id;
                $data['file'] = $info->file;
                $data['train'] = $info->train;
                //修改训练中
                ModelVersion::where('id',$info->id)->update([
                    'status'=>1,
                    'start_date' => date('Y-m-d H:i:s')
                ]);
            }
            DB::commit();
            return ToolsService::ok($data);
        }catch (Exception $e){
            DB::rollBack();
            return ToolsService::ok();
        }
    }

    /**
     * 获取已训练完成数据
     */
    public function getFinishInfo(Request $request){
        try{
            DB::beginTransaction();
            $data = [];

            //获取待训练详情
            $info = ModelVersion::where('status',4)->lockForUpdate()->first();
            if($info){
                $data['id'] = $info->id;
                $data['train'] = $info->train;

                //模型转换中
                ModelVersion::where('id',$info->id)->update([
                    'status'=>5,
                    'end_date' => date('Y-m-d H:i:s')
                ]);
            }

            DB::commit();
            return ToolsService::ok($data);
        }catch (Exception $e){
            DB::rollBack();
            return ToolsService::ok();
        }
    }

    /**
     * 设置训练详情
     */
    public function setInfo(Request $request){
        $id = $request->input('id',0);
        $params['status'] = $request->input('status',0);
        $params['remark'] = $request->input('remark','');
        $params['train_file'] = $request->input('train_file','');
        $params['batch_img'] = $request->input('batch_img','');
        $params['end_date'] = date('Y-m-d H:i:s');
        $params['tag'] = $request->input('tag','');

        if(isset($params['train_file']) && !$params['train_file']){
            unset($params['train_file']);
        }

        if(isset($params['batch_img']) && !$params['batch_img']){
            unset($params['batch_img']);
        }

        if(isset($params['tag']) && !$params['tag']){
            unset($params['tag']);
        }

        ModelVersion::where('id',$id)->update($params);

        //如果训练完成同步训练好的模型到主表
        if($params['status'] == 2){
            $info = ModelVersion::where('id',$id)->first();
            if($info){
                ModelList::where('id',$info->model_id)->update([
                    'train_file'=>$params['train_file'],
                    'train_version'=>$info['version']
                ]);
            }
        }

        return ToolsService::ok();
    }
}
